package com.wrs.examsystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wrs.examsystem.entity.Answer;
import com.wrs.examsystem.entity.Result.R;
import com.wrs.examsystem.entity.Test;
import com.wrs.examsystem.entity.Topic;
import com.wrs.examsystem.entity.bean.StudentMark;
import com.wrs.examsystem.mapper.AnswerMapper;
import com.wrs.examsystem.mapper.StudentMapper;
import com.wrs.examsystem.mapper.TestMapper;
import com.wrs.examsystem.mapper.TopicMapper;
import com.wrs.examsystem.service.StudentService;
import com.wrs.examsystem.service.TestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wrs.examsystem.util.wheel.StudentUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 王荣升
 * @since 2024-05-15
 */
@Service
public class TestServiceImpl extends ServiceImpl<TestMapper, Test> implements TestService {

    @Resource
    TestMapper testMapper;

    @Resource
    TopicMapper topicMapper;

    @Resource
    StudentMapper studentMapper;

    @Resource
    AnswerMapper answerMapper;

    @Resource
    StudentService studentService;

    @Override
    public R findTest(Integer testId) {
        QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
        topicQueryWrapper.eq("test_id",testId);
        List<Topic> topics = topicMapper.selectList(topicQueryWrapper);
        if (topics.isEmpty()){
            return R.error().message("该试卷不存在");
        }
        topics.sort(Comparator.comparingInt(Topic::getTopicNumber));
        return R.ok().data(topics);
    }

    @Override
    public R findTests(int[] testIds) {
        QueryWrapper<Test> testQueryWrapper = new QueryWrapper<>();
        Arrays.sort(testIds);
        testQueryWrapper.le("test_id",testIds[testIds.length-1]);
        testQueryWrapper.le("test_id",testIds[0]);
        List<Test> tests = testMapper.selectList(testQueryWrapper);
        if (tests.isEmpty()){
            return R.error();
        }
        return R.ok().data(tests);
    }

    @Override
    public R findBySubject(String subject) {
        QueryWrapper<Test> testQueryWrapper = new QueryWrapper<>();
        testQueryWrapper.eq("subject",subject);
        List<Test> tests = testMapper.selectList(testQueryWrapper);
        if (tests == null){
            return R.error();
        }
        return R.ok().data(tests);
    }

    @Override
    public R deleteTest(Integer testId) {
//        先删除试卷
        int deleteById = testMapper.deleteById(testId);
        SQLException exception = new SQLException();
//        删除试卷中的题目
        QueryWrapper<Topic> topicQueryWrapper = new QueryWrapper<>();
        topicQueryWrapper.eq("test_id",testId);
        int delete = topicMapper.delete(topicQueryWrapper);
        if (deleteById == 0 || delete == 0){
            exception.printStackTrace();
        }
        return R.ok();
    }

    @Override
    public R insertTests(Test test) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
//        System.out.println(formatter.format(date));
        test.setCreateTime(formatter.format(date));
        testMapper.insert(test);
        return R.ok();
    }

    @Override
    public R findStudentMark(Integer testId) {
//        根据试卷ID查询answer表    得到所有的答案集合
        QueryWrapper<Answer> answerQueryWrapper = new QueryWrapper<>();
        answerQueryWrapper.eq("test_id",testId);
        List<Answer> answers = answerMapper.selectList(answerQueryWrapper);
        Test test = testMapper.selectById(testId);
//        将studentNumber单独拎出来进行计算总分放到一个集合中
        StudentMark studentMark;
        List<StudentMark> studentMarks = new ArrayList<>();
        HashSet<Integer> studentIds = new HashSet<>();
        for (int i = 0; i < answers.size(); i++) {
            int studentId = answers.get(i).getStudentId();
            if (studentIds.add(studentId)){
                studentMark = StudentUtil.studentInvertStudentMark(studentMapper.selectById(studentId));
                studentMarks.add(studentMark);
            }
        }
        for (int i = 0; i < studentIds.size(); i++) {
            Integer totalMark = studentService.findTotalMark(studentMarks.get(i).getStudentNumber(), testId);
            studentMarks.get(i).setSubject(test.getSubject());
            studentMarks.get(i).setTotalMark(totalMark);
        }
//        排序返回集合
        return R.ok().data(studentMarks);
    }


//    @Override
//    public R findStudentGrade(Integer teacherId) {
//
////        studentService.findTotalMark();
//    }

}
